AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "apc_engine_start" )
util.PrecacheSound( "apc_engine_stop" )

include('shared.lua')

local Energy_Increment = 100
local Water_Increment = 50
local Air_Increment = 25

function ENT:Initialize()
	self.BaseClass.Initialize(self)
	self.Active = 0
	self.overdrive = 0
	self.multip = 1
	self.snd = 1
	self.damaged = 0
	self.lastused = 0
	if not (WireAddon == nil) then
		self.WireDebugName = self.PrintName
		self.Inputs = Wire_CreateInputs(self.Entity, { "On", "Overdrive", "Multiplier", "Mute" })
		self.Outputs = Wire_CreateOutputs(self.Entity, {"On", "Overdrive" })
	end
end

function ENT:TurnOn()
	if (self.Active == 0) then
		if (self.snd > 0) then
			self.Entity:EmitSound( "Airboat_engine_idle" )
		end
		self.Active = 1
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", self.Active) end
		self:SetOOO(1)
	elseif ( self.overdrive == 0 ) then
		self:TurnOnOverdrive()
	end
end

function ENT:TurnOff()
	if (self.Active == 1) then
		if (self.snd > 0) then
			self.Entity:StopSound( "Airboat_engine_idle" )
			self.Entity:EmitSound( "Airboat_engine_stop" )
		end
		self.Entity:StopSound( "apc_engine_start" )
		self.Active = 0
		self.overdrive = 0
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", self.Active) end
		self:SetOOO(0)
	end
end

function ENT:TurnOnOverdrive()
	if ( self.Active == 1 ) then
		if (self.snd > 0) then
			self.Entity:StopSound( "Airboat_engine_idle" )
			self.Entity:EmitSound( "Airboat_engine_idle" )
		end
		self.Entity:EmitSound( "apc_engine_start" )
		self:SetOOO(2)
		self.overdrive = 1
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Overdrive", self.overdrive) end
	end
end

function ENT:TurnOffOverdrive()
	if ( self.Active == 1 and self.overdrive == 1) then
		if (self.snd > 0) then
			self.Entity:StopSound( "Airboat_engine_idle" )
			self.Entity:EmitSound( "Airboat_engine_idle" )
		end
		self.Entity:StopSound( "apc_engine_start" )
		self:SetOOO(1)
		self.overdrive = 0
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Overdrive", self.overdrive) end
	end	
end

function ENT:SetActive( value )
	if (value) then
		if (value != 0 and self.Active == 0 ) then
			self:TurnOn()
		elseif (value == 0 and self.Active == 1 ) then
			self:TurnOff()
		end
	else
		if ( self.Active == 0 ) then
			self.lastused = CurTime()
			self:TurnOn()
		else
			if ((( CurTime() - self.lastused) < 2 ) and ( self.overdrive == 0 )) then
				self:TurnOnOverdrive()
			else
				self.overdrive = 0
				self:TurnOff()
			end
		end
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		self:SetActive(value)
	elseif (iname == "Overdrive") then
		if (value ~= 0) then
			self:TurnOnOverdrive()
		else
			self:TurnOffOverdrive()
		end
	elseif (iname == "Multiplier") then
		if (value > 0) then
			self.multip = value
		end
	elseif (iname == "Mute") then
		self.snd = value
	end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
	if ((self.Active == 1) and (math.random(1, 10) <= 4)) then
		self:TurnOff()
	end
end

function ENT:Repair()
	self.BaseClass.Repair(self)
	self.Entity:SetColor(255, 255, 255, 255)
	self.damaged = 0
end

function ENT:Destruct()
	if CAF and CAF.GetAddon("Life Support") then
		CAF.GetAddon("Life Support").Destruct( self.Entity, true )
	end
end

function ENT:OnRemove()
	self.BaseClass.OnRemove(self)
	self.Entity:StopSound( "apc_engine_start" )
end

function ENT:Proc_Water()
	local RD = CAF.GetAddon("Resource Distribution")
	local energy = RD.GetResourceAmount(self, "energy")
	local water = RD.GetResourceAmount(self, "water")
	local einc = (Energy_Increment + (self.overdrive*Energy_Increment) * self.multip)
	einc = math.Round(einc * self:GetMultiplier())
	local winc = (Water_Increment + (self.overdrive*Water_Increment) * self.multip)
	winc = math.Round(winc * self:GetMultiplier())

	if (energy >= einc and water >= winc) then
		if ( self.overdrive == 1 ) then
			if CAF and CAF.GetAddon("Life Support") then
				CAF.GetAddon("Life Support").DamageLS(self, math.random(2, 3))
			else
				self:SetHealth( self:Health( ) - math.Random(2, 3))
				if self:Health() <= 0 then
					self:Remove()
				end
			end
		end
		RD.ConsumeResource(self, "energy", einc)
		RD.ConsumeResource(self, "water", winc)
		local leftO2 = RD.SupplyResource(self, "oxygen", math.Round((Air_Increment + (self.overdrive * Air_Increment ))*self:GetMultiplier() ) * self.multip)
		local leftH = RD.SupplyResource(self, "hydrogen", math.Round((Air_Increment + (self.overdrive * Air_Increment ))*self:GetMultiplier() ) * 2 * self.multip)
		if self.environment then
			self.environment:Convert(-1, 0, leftO2)
			self.environment:Convert(-1, 3, leftH)
		end
	else
		self:TurnOff()
	end
end

function ENT:Think()
	self.BaseClass.Think(self)
	
	if ( self.Active == 1 ) then self:Proc_Water() end
	
	self.Entity:NextThink( CurTime() + 1 )
	return true
end